<template>
  <div class="ver-layout-center-all sql-layout">
    <el-input class="sql-input" type="textarea" v-model="relationDataStr" @change="inputChange"></el-input>
    <base-level class="level-layout" ref="baseLevelRef" v-model="relationData" @levelBack="levelBack"></base-level>
    <el-button style="margin-top: 20px" type="danger" size="small" @click="clearData">清空数据</el-button>
  </div>
</template>

<script>
import BaseLevel from "@/views/relation/base-level";
import {genRelationByString} from "@/utils/relation_utils";

export default {
  name: "relation",
  components: {BaseLevel},
  data() {
    return {
      relationData: {},
      relationDataStr: "( ( #a < 3 or #u3 == 4 ) and ( #w < 3 and #zz < 2 and #hg < 4 ) or ( #e contains 3 or #d == 2 ) ) and ( ( #b == 5 and #m !== 1 ) or #z > 3 )"
    }
  },
  mounted() {
    this.relationData = genRelationByString(this.relationDataStr)
    // if (!this.relationData.id) this.$refs.baseLevelRef.initData()
    // if (this.relationData.id) this.generateRelationStr()
  },
  methods: {
    inputChange() {
      this.relationData = genRelationByString(this.relationDataStr)
    },
    levelBack(val) {
      this.relationData = val
      this.generateRelationStr()
    },
    clearData() {
      this.relationData = {}
      this.$nextTick(() => this.$refs.baseLevelRef.initData())
    },
    generateRelationStr() {
      console.log("this.relationData", JSON.stringify(this.relationData))
      this.relationDataStr = this.getRuleStr(this.relationData)
      this.relationDataStr = this.relationDataStr.indexOf("(") != -1 ? this.relationDataStr.substring(1, this.relationDataStr.length - 1) : this.relationDataStr
    },
    getRuleStr(val, str = '') {
      if (!val.children) return val.valueStr
      let relationList = val.children.map(item => {
        if (item.model == 'relationCon') return this.getRuleStr(item, str)
        else return item.valueStr || item.command
      })
      str += `( ${relationList.join(" ")} )`
      return str
    }
  }
}
</script>

<style scoped>
.sql-input {
  margin-top: 200px;
  margin-bottom: 10px;
  font-size: 16px;
  font-weight: bold;
  background: #c5c5c5;
  padding: 10px;
  border-radius: 10px;
  font-family: Consolas;
  font-weight: bold;
}

.level-layout {
  margin-top: 30px;
  width: calc(90vw - 60px);
  padding: 30px;
  overflow-x: scroll;
  background: #c5c5c5;
  border-radius: 10px;
}

.level-layout::-webkit-scrollbar {
  width: 7px;
}

.level-layout::-webkit-scrollbar-track {
  background: transparent;
}

.level-layout::-webkit-scrollbar-thumb {
  background-color: rgb(138, 170, 192);
  border-radius: 7px;
  position: absolute;
  top: -2px;
  left: 2px;
  right: 2px;
}
</style>
